package com.joint.web.action.mp;

import com.fz.us.base.bean.BaseEnum;
import com.fz.us.base.bean.Pager;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.joint.base.util.StringUtils;
import com.joint.core.entity.Linkman;
import com.joint.core.service.LinkmanService;
import com.joint.web.action.BaseController;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by dqf on 2015/3/10.
 */
@Controller("mpLinkman")
@RequestMapping("/mp")
public class AjaxLinkmanController extends BaseController {
    @Resource
    private LinkmanService linkmanService;

    private List<Linkman> linkmanList;
    private Linkman linkman;
    private String searchString;

    @RequestMapping(value = "/ajax-linkman/list", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> list(int page,String pageKeyword)
    {
        List<Map<String, Object>> dataRows = Lists.newArrayList();
        JSONObject data =new JSONObject();
        Map<String, Object> rmap;
        pager = new Pager();
        pager.setPageSize(10);
        pager.setPageNumber(page);
        pager.setOrderBy("createDate");
        pager.setOrderType(BaseEnum.OrderType.desc);
        Map<String, Object> map;
        if (StringUtils.isNotEmpty(pageKeyword)){
            map = Maps.newHashMap();
            map.put("state", new BaseEnum.StateEnum[]{BaseEnum.StateEnum.Enable});
            map.put("name", pageKeyword);
            pager = linkmanService.findByPagerAndCompany(pager,null,usersService.getCompanyByUser(),map);
        }
        else{
            pager = linkmanService.findByPagerAndCompany(pager,null,usersService.getCompanyByUser(),new BaseEnum.StateEnum[]{BaseEnum.StateEnum.Enable});
        }

        linkmanList = (List<Linkman>) pager.getList();
        for(Linkman linkman:linkmanList){
            rmap=Maps.newHashMap();
            rmap.put("url",getPath()+"mp/ajax-linkman/read/"+linkman.getId());
            rmap.put("name",linkman.getName());
            rmap.put("subName",linkman.getSubName());
            rmap.put("mobile",linkman.getMobile());
            dataRows.add(rmap);
        }
        data.put("pageNum", pager.getPageNumber());//页数
        data.put("pageSize", dataRows.size());//记录数
        data.put("pageCount", pager.getPageCount());//总页数
        data.put("totalCount", pager.getTotalCount());//总记录数
        data.put("pageKeyword", pager.getKeyword()==null?"": pager.getKeyword());//查询
        data.put("dataRows", dataRows);//数据
        return ajaxHtmlAppResult(1,"",data);
    }

    public String read(){
        if(StringUtils.isEmpty(keyId)) return "index";
        linkman = linkmanService.get(keyId);
        return "read";
    }

    public List<Linkman> getLinkmanList() {
        return linkmanList;
    }

    public void setLinkmanList(List<Linkman> linkmanList) {
        this.linkmanList = linkmanList;
    }

    public Linkman getLinkman() {
        return linkman;
    }

    public void setLinkman(Linkman linkman) {
        this.linkman = linkman;
    }

    public String getSearchString() {
        return searchString;
    }

    public void setSearchString(String searchString) {
        this.searchString = searchString;
    }
}
